<?php

namespace App\Http\Controllers\Admin;

use App\Models\CommunityData;
use App\Services\Admin\CommunityService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CommunityDataController extends Controller
{
    protected $service;

    public function __construct(CommunityService $service)
    {
        $this->service = $service;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $community_id = $request->get('community_id')??0;
        $community = '';
        if($community_id){
            $community = $this->service->getCommunityById($community_id);
        }
        return view('admin.community_data.index',compact('community_id','community'));
    }

    public function data(Request $request)
    {
        $model = CommunityData::query();
        $res = $model->orderBy('community_id','desc')->with(['community'])->paginate($request->get('limit',30))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $community_id = $request->get('community_id');
        $community = $this->service->getCommunityById($community_id);
        return view('admin.community_data.create',compact('community','community_id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->all();
        CommunityData::create($data);
        return redirect(route('admin.community_data'))->with(['status'=>'添加成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = $this->service->getCommunityDataById($id);
        if (!$data){
            return redirect(route('admin.community_data'))->withErrors(['status'=>'记录不存在']);
        }
        $community = $this->service->getCommunityById($data->community_id);
        $community_id = $community->id;

        return view('admin.community_data.edit',compact('data','community','community_id'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $community_data = $this->service->getCommunityDataById($id);
        $data = $request->all();

        if ($community_data->update($data)){
            return redirect(route('admin.community_data'))->with(['status'=>'更新成功']);
        }
        return redirect(route('admin.community_data'))->withErrors(['status'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        foreach (CommunityData::whereIn('id',$ids)->get() as $model){
            //删除图片
            $model->delete();
        }
        return response()->json(['code'=>0,'msg'=>'删除成功']);
    }
}
